package org.jpass.dc.view.user;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.jpass.dc.business.user.bean.UserVOBean;
import org.jpass.dc.business.user.logic.UserManageLogic;
import org.jpass.dc.business.user.logic.UserManageLogicImpl;
import org.jpass.dc.common.util.Encrypter;
import org.jpass.dc.common.util.StringUtil;
import org.jpass.dc.view.common.BasicAction;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class ModifyPasswordAction extends BasicAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4490441343393485369L;
	
	private Log log = LogFactory.getLog(ModifyPasswordAction.class);
	
	private String newPassword;
	private String okPassword;
	private String oldPassword;
	
	private UserManageLogic userManage = new UserManageLogicImpl();
	
	public String getNewPassword() {
		return newPassword;
	}
	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}
	public String getOkPassword() {
		return okPassword;
	}
	public void setOkPassword(String okPassword) {
		this.okPassword = okPassword;
	}
	public String getOldPassword() {
		return oldPassword;
	}
	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}
	
	@Override
	public String execute() throws Exception {
		String resJson = null;
		String msg = this.validateData();
		if(msg == null) {
			UserVOBean user = (UserVOBean) this.getCurrentUser();
			try {
				if (userManage.resetPass(user.getId(), Encrypter.md5Encrypt(newPassword))) {
					user.setPassword(Encrypter.md5Encrypt(newPassword));
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("success", true);
					map.put("msg", "Modify successfully.");
					map.put("user", user);
					Gson gson= new GsonBuilder().create();
					resJson = gson.toJson(map);
				}
			} catch (Exception e) {
				resJson = "{'success':false, 'msg':'" + e.getMessage() + "', 'type':'error'}";
			}
		} else {
			resJson = "{'success':false, 'msg':'" + msg + "', 'type':'error'}";
		}
		try {
			this.getResponse().setCharacterEncoding("utf-8");
			ServletActionContext.getResponse().getWriter().print(resJson);
		} catch (IOException e) {
			e.printStackTrace();
			log.error(this.getCurrentUser().getUsername() + "请求用户信息获取PrintWriter时抛出异常，异常信息：\n", e);
		}
		
		return null;
	}
	
	/**
	 * 验证客户端数据
	 * @return
	 * @throws Exception
	 */
	protected String validateData() throws Exception {
		if (StringUtil.isBlank(newPassword)) {
			return "New password is blank.";
		}else if (!newPassword.matches("^[a-zA-Z0-9]{6,18}$")) {
			return "The new password is Illegal.";
		}
		if (!newPassword.equals(okPassword)) {
			return "Two of the input password is not the same.";
		}
		if (!this.getCurrentUser().getPassword().equals(Encrypter.md5Encrypt(oldPassword))) {
			return "The old password is wrong.";
		}
		return null;
	}
}
